package com.example.ebeanbase.controller;

import com.example.ebeanbase.common.base.BaseController;
import com.example.ebeanbase.entity.User;
import com.example.ebeanbase.request.LoginParam;
import com.example.ebeanbase.response.ApiResponseEnum;
import com.example.ebeanbase.response.BaseRet;
import com.example.ebeanbase.utils.CommonFunctionUtil;
import com.example.ebeanbase.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

/**
 * @program: ebean-base
 * @description:
 * @author: king
 * @date: 2020-09-25 20:59
 **/
@RestController
@Api("demo")
public class DemoController extends BaseController {

    private static final Logger logger=  LoggerFactory.getLogger(DemoController.class);

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value="不需要赋权的接口")
    public BaseRet<User> login(@RequestBody LoginParam loginParam ){
        BaseRet<User> ret = new BaseRet<>();
        if(StringUtil.isBlank(loginParam.getUsername())|| StringUtil.isBlank(loginParam.getPassword())){
            ret.setMsg("用户名或密码不能为空");
            return ret;
        }

        User user = userService.selectUserByUP(loginParam.getUsername(), CommonFunctionUtil.string32MD5(loginParam.getPassword()));
        if(StringUtil.isBlank(user)){
            ret.setMsg("系统无此用户");
            return ret;
        }
        user.setToken(CommonFunctionUtil.createUuid());
        userService.loginSuccess(user);
        ret.setMsg("登录成功");
        ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
        ret.setData(user);
        return ret;
    }

    @RequestMapping(value = "/doAuth/getUser",method = RequestMethod.POST)
    @ApiOperation(value="需要赋权的接口")
    public BaseRet<User> getUser(@RequestBody LoginParam loginParam ){
        BaseRet<User> ret = new BaseRet<>();
        if(StringUtil.isBlank(loginParam.getUsername())|| StringUtil.isBlank(loginParam.getPassword())){
            ret.setMsg("用户名或密码不能为空");
            return ret;
        }

        User user = userService.selectUserByUP(loginParam.getUsername(), CommonFunctionUtil.string32MD5(loginParam.getPassword()));
        if(StringUtil.isBlank(user)){
            ret.setMsg("系统无此用户");
            return ret;
        }
        user.setToken(CommonFunctionUtil.createUuid());
        userService.loginSuccess(user);
        ret.setMsg("登录成功");
        ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
        ret.setData(user);
        return ret;
    }
}
